package com.example.randox;

import java.util.HashMap;

import library.DatabaseHandler;
import library.UserFunctions;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class SplashScreen extends Activity {

	private static String KEY_SUCCESS = "success";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_splash_screen);

		// METHOD 1

		/****** Create Thread that will sleep for 5 seconds *************/
		Thread background = new Thread() {
			public void run() {

				try {
					sleep(3 * 1000);

					DatabaseHandler db = new DatabaseHandler(getApplicationContext());
					HashMap<String, String> userDetails = db.getUserDetails();

					UserFunctions userFunction = new UserFunctions();

					if (db.getRowCount() > 0) {

						JSONObject json = userFunction.checkConversation(userDetails.get("email").toString());
						try {
							if (json.getString(KEY_SUCCESS) != null) {
								String res = json.getString(KEY_SUCCESS);
								if (Integer.parseInt(res) == 1) {
									Intent i = new Intent(SplashScreen.this, ChatActivity.class);
									i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
									startActivity(i);
									finish();
								} else {
									Intent i = new Intent(SplashScreen.this, MemberActivity.class);
									i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
									startActivity(i);
									finish();
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}

					} else {
						Intent i = new Intent(SplashScreen.this, LoginActivity.class);
						i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
						startActivity(i);
						finish();
					}

				} catch (Exception e) {

				}
			}
		};
		background.start();

	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {

	}

}